#By Daoyu 2011-10-03
from django.shortcuts import render_to_response
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.template import RequestContext
from django.contrib.auth.models import User

from uncle.project.models import *
from uncle.util.page import *
from uncle.util.util import *
from uncle.users.models import *
from uncle.util.util import match_email_address

import re

prefix = '/home/b25476/uncle/'

def delete_basket(request, bid):
    if request.user.username == 'kth' or Basket.objects.get(id=bid).project.creator.id == request.user.id:
        Basket.objects.filter(id=bid).delete()        
        request.user.message_set.create(message='Basket updated.')
    return HttpResponse("success")

def hide_basket(request, bid):
    if request.user.username == 'kth' or Basket.objects.get(id=bid).project.creator.id == request.user.id:
        basket = Basket.objects.get(id=bid)
        basket.hide = True
        basket.save()
    return HttpResponse("success")

def show_basket(request, bid):
    if request.user.username == 'kth' or Basket.objects.get(id=bid).project.creator.id == request.user.id:
        basket = Basket.objects.get(id=bid)
        basket.hide = False
        basket.save()
    return HttpResponse("success")

def edit_mini_basket(request, bid):
    basket = Basket.objects.get(id=bid)
    return render_to_response('project/edit_mini_basket.html',
              {'basket':basket},
              context_instance = RequestContext(request))

def mini_basket(request, bid):
    basket = Basket.objects.get(id=bid)
    return render_to_response('project/view_mini_basket.html',
              {'basket':basket},
              context_instance = RequestContext(request))
    
def edit_basket(request, bid):
    if request.method == 'POST' and request.user.username == 'kth' or Basket.objects.get(id=bid).project.creator.id == request.user.id:
        basket = Basket.objects.get(id=bid)
        basket.title = request.POST.get('title')
        basket.text = request.POST.get('text')
        basket.save()
        request.user.message_set.create(message='Basket updated.')
    return HttpResponse("success")

def __youtube_id(original):
    youtubeExp = '\?v=(?P<yid>[^&]*)'
    
    yid = re.compile(youtubeExp).search(original)
    if yid:
        return yid.group("yid")
    else:
        return "WFHJkvEwyhk"

if __name__ == "__main__":
    print __youtube_id("www.youtube.com/watch?v=WFHJkvEwyhk_&__")

def new_basket(request, pid):
    project = Project.objects.get(id=pid)
    if request.user.username == 'kth' or project.creator.id == request.user.id:
        b = Basket()
        b.title = request.POST.get('title')
        b.text = request.POST.get('text')
        b.user = request.user
        b.project = project
        b.hide = True
        
        type = request.POST.get('type')        
        
        if type == "upload":
            if(request.FILES.has_key("asset")): 
                from uncle.users.lib.file import upload as upload_assets           
                _type, _path = upload_assets(request.FILES["asset"], prefix, request.user.id)
                
                b.type = _type
                b.url = _path
        elif type == "youtube":
            b.type = 4
            b.url = __youtube_id(request.POST.get('youtube_url'))
                    
        b.save()
        moods = request.POST.get('moods')
        sb = Skill_basket()
        sb.basket = b
        sb.skill = Skill.objects.get(id=moods)
        sb.save()
    return HttpResponseRedirect("/project/%s/edit/" % pid)